package com.romotc.qqrobot.webqq.qqInterfaceCall;

import java.util.ArrayList;
import java.util.Collection;

import org.json.JSONException;
import org.json.JSONObject;

import com.romotc.qqrobot.bean.QQLogedin;
import com.romotc.qqrobot.webqq.qqInterfaceCall.dataProcessor.Getimage_Processor;
import com.romotc.scache.httpreq;

/**
 * 根据QQ号码 或者 QQ号码主邮箱 获取对应的UIn
 * 必须传入的参数 JSONObject:{Key_Input_Tuin: QQ号码或者QQ号码主邮箱}
 * @author Administrator
 *
 */
public class Search_qq_by_uin2_InterCall extends QQInterfaceCall{

	private static Search_qq_by_uin2_InterCall instance = new Search_qq_by_uin2_InterCall();
	private static String Key_Search_QQNum = "SearchQQNum";
	
	public static final int req_method = httpreq.REQUEST_GET;
	public static final String reqUrl = "http://s.web2.qq.com/api/search_qq_by_uin2";
	private static final int logLevel = 1001;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public static Search_qq_by_uin2_InterCall getInstance() {
		return instance;
	}

	/**
	 * 验证码
	 */
	public static final String Key_VerifyCode = "verifyCode";
	
	/**
	 * 必须传入的内容 需要查询的号码
	 */
	public static String Key_Input_Tuin = "Key_Input_Tuin";

	@Override
	public String buildReqBody(JSONObject reqDataObj, QQLogedin loginQQ) {
		// get方式没有reqBody
		return null;
	}



	@Override
	/**
Accept:
Accept-Charset:UTF-8,*;q=0.5
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-CN,zh;q=0.8
Content-Type:utf-8
Cookie:pgv_pvid=4777447478; pgv_info=pgvReferrer=&ssid=s2968728146; ptui_loginuin=1586030808; pt2gguin=o1586030808; uin=o1586030808; skey=@Jf2uW5KKq; ptisp=cnc; show_id=; ptwebqq=109e01d42d44d28516a1df27f327e920bad29e386329c29ab2f0f80c9bbe6b86; verifysession=h00264df36e58628c8524d7738f09ca8592881ca043b89b1a8385171c208a2d854b2ecec7975374c8b9
Host:s.web2.qq.com
Proxy-Connection:keep-alive
Referer:http://s.web2.qq.com/proxy.html?v=20110412001&callback=1&id=1
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7
	 */
	public ArrayList<String> myReqHeaders(int reqBodyLen, QQLogedin loginQQ,JSONObject reqDataObj) {
		return buildReqHeaders(loginQQ, 
				"http://s.web2.qq.com/proxy.html?v=20110412001&callback=1&id=1", "s.web2.qq.com");
	}
	
	
	/**
	 * 调用getImages接口获取验证码
	 */
	@Override
	public JSONObject preCall(QQLogedin loginQq, JSONObject reqDataObj) throws Exception {
		QQInterfaceCall call = GetImage_InterCall.getInstance();
		JSONObject jsonRtInfo = call.call(loginQq, reqDataObj, Getimage_Processor.getInstance());
//getImageProcessor中已经设置	reqDataObj.put(Getimage_Processor.Key_VerifySession, jsonRtInfo.get(Getimage_Processor.Key_VerifySession));
		reqDataObj.put(Key_VerifyCode, distinguishVerifyImage(jsonRtInfo));
		
		//输入验证码的时间 
		Thread.sleep(4000);
		
		return reqDataObj;
	}

	/**
	 * 识别验证码图片中的内容 以后可以增加智能识别 或者 人工识别
	 * @param jsonRtInfo
	 * @return
	 */
	private String distinguishVerifyImage(JSONObject jsonRtInfo) {
		// TODO Auto-generated method stub
		
		return "dqWq";
	}

	public int getReq_method() {
		return req_method;
	}

	/**
	 * http://s.web2.qq.com/api/search_qq_by_uin2?tuin=13361004869
	 * 	&verifysession=h00ee71a840e3816c602f52612762ca99f80bc5aede12dd7b95086abd87cc3b28e2cdf30a9ff5734c4f
	 *  &code=rurw
	 *  &vfwebqq=096f2f3924fee7c34387eb902ac33c0881e5c8b9791d031cd68c6d2522a389f17b8724ff23d55a56
	 *  &t=1326680876695
	 * @throws JSONException 
	 */
	public String getReqUrl(JSONObject reqDataObj, QQLogedin loginQQ) throws JSONException {
		StringBuilder bld = new StringBuilder(reqUrl);
		bld.append("?tuin=").append(reqDataObj.get(Key_Input_Tuin ));
		bld.append("&verifysession=" + reqDataObj.getString(Getimage_Processor.Key_VerifySession));
		bld.append("&code=").append(reqDataObj.getString(Key_VerifyCode));
		bld.append("&vfwebqq=").append(loginQQ.getVfwebqq());
		bld.append("&t=").append(System.currentTimeMillis());
		
		return bld.toString();
	}

	public int getLogLevel() {
		return logLevel;
	}

	@Override
	public boolean hasPreCall(QQLogedin loginQQ, JSONObject reqDataObj) {
		// TODO Auto-generated method stub
		return true;
	}

	
}
